import java.util.Arrays;

public class Test {
    public static void main1(String[] args) {
        int[] array={1,2,3,4,5};
        System.out.println(array[2]);
        System.out.println(array[4]);
        System.out.println("===普通for循环===");
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i]+" ");
        }
        System.out.println();
        System.out.println("===for each===");
        for (int e: array) {
            System.out.print(e+" ");
        }
        System.out.println();
        System.out.println("===操作数组的工具类Arrays===");
        String ret= Arrays.toString(array);
        System.out.println(ret);

        int[] array2=new int[]{1,2,3,4,5};
        int[] array3=new int[10];
        int[] array4;
        array4=new int[10];
        int[] array5;
        array5=new int[]{1,2,3,4,5};
        //int[] array6;
        //array6={1,2,3,4,5};
        boolean[] array7=new boolean[3];
        //System.out.println(array7[2]);
    }

    public static void main2(String[] args) {
        int[] array={1,2,3,4,5};
        fun1();
    }
    public static void fun1(){
        int a=10;
        int b=20;
        int[] array={1,2,3};
        int[] array2=null;
        System.out.println(array2.length);
    }
   //需求：把数组中每个数据扩大2倍
    /*public static int[] fun(int[] array){
        int[] ret=new int[array.length];
        for (int i = 0; i < array.length ; i++) {
            ret[i]=array[i]*2;
        }
        return ret;//函数结束:意味着回收局部变量的内存
    }
    public static void main(String[] args) {
        int[] array={1,2,3,4,5};
        int[] tmp=fun(array);
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(tmp));
    }*/
   public static void fun2(int[] array){

       for (int i = 0; i < array.length ; i++) {
           array[i]=array[i]*2;
       }

   }
   public static void fun3(int[] array){
       array[0]=99;
   }
    public static void fun4(int[] array){
        array=new int[]{99,2,3,4,5};
    }
    public static void main3(String[] args) {
        int[] array={1,2,3,4,5};
        System.out.println(Arrays.toString(array));
        fun4(array);
        System.out.println(Arrays.toString(array));
        fun3(array);
        System.out.println(Arrays.toString(array));

    }

    public static String toMyString(int[] array){
       String string="";
       string+="[";
        for (int i = 0; i < array.length-1; i++) {
            string+=array[i]+",";
        }
        string+=array[array.length-1];
        string+="]";
        return string;
    }
    public static void main4(String[] args) {
        int[] array={1,2,3,4,5};
        String string=toMyString(array);
        System.out.println(string);
    }

    public static int findNum(int[] array,int key){
        int index=-1;
       for (int i = 0; i < array.length ; i++) {
            if(array[i]==key){
                index=i;
            }
        }
      return index;
    }
    public static void main5(String[] args) {
        int[] array={1,2,3,4,5};
        Arrays.sort(array);
        int index=binarySearch(array,5);
        int indexx=Arrays.binarySearch(array,6);
        System.out.println(index);
        System.out.println(indexx);
    }
    public static int binarySearch(int[] array,int key){
       int left=0;
       int right=array.length-1;
       while(left<=right){
           int mid=(left+right)/2;
           if(array[mid]==key){
               return mid;
           }else if(key<array[mid]){
               right=mid-1;
           }else{
               left=mid+1;
           }
       }
       return  -1;
    }
    public static int[] bubbleSort(int[] array,int key){
        for (int i = 0; i < array.length-1; i++) {
            boolean flag=false;
            for (int j = 0; j <array.length-1-i ; j++) {
                if(array[j]<array[j+1]){
                    int tmp=array[j];
                    array[j]=array[j+1];
                    array[j+1]=tmp;
                    flag=true;
                }
            }
            if(flag==false){
                break;
            }
        }
        return array;
    }

    public static void main6(String[] args) {
        int[] array={1,2,3,4,5};
        int[] newArray=array;
        newArray[0]=10;
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(newArray));
    }

    public static void main7(String[] args) {
        int[][] array=new int[][]{{1,2,3},{4,5,6}};
        System.out.println(array.length);//行数
        System.out.println(array[0].length);//一行有吉列
        System.out.println(array[1].length);
        System.out.println(array);
        System.out.println(array[0]);
        System.out.println(array[1]);
        for (int i = 0; i < array.length ; i++) {
            for (int j = 0; j <array[i].length ; j++) {
                System.out.print(array[i][j]+" ");
            }
        }
        System.out.println();
        System.out.println("=================");
        for (int[] i:array) {
            for (int x:i) {
                System.out.print(x+" ");
            }
        }
        System.out.println();
        System.out.println("=================");
        String ret=Arrays.toString(array);
        System.out.println(ret);
        System.out.println("=================");
        String rett=Arrays.deepToString(array);
        System.out.println(rett);
    }

    public static void main(String[] args) {
        int[][] array=new int[2][];
        for (int i = 0; i <array.length ; i++) {
            for (int j = 0; j <array[i].length ; j++) {
                System.out.println(array[i][j]+" ");
            }
        }
    }
}
